package com.gxuwz.medical.domain.menu;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;

public class MenuTree {
	private List<MenuTreeNode> ret;//存储构造完成之后的菜单
	
	public MenuTree(Menu menu) throws SQLException{//构造一个根节点为menuid的权限树
		ret = new ArrayList<MenuTreeNode>();
		List<Menu> list = menu.findChildren();
		for(Menu m:list)
			ret.add(buildtree(m));
	}
	public MenuTree(Menu menu,String roleid) throws SQLException{//构造一个基于角色的、根节点为menuid的权限树
		ret = new ArrayList<MenuTreeNode>();
		List<Menu> list = menu.findChildren();
		for(Menu m:list)
			ret.add(buildtree(m,roleid));
	}
	
	public MenuTreeNode buildtree(Menu m) throws SQLException{//递归生成不带角色的权限树
		MenuTreeNode node = new MenuTreeNode(m);
		List<Menu> children = new ArrayList<Menu>();
		children = m.findChildren();
		if(children.size()!=0){
			List<MenuTreeNode> childrens = new ArrayList<MenuTreeNode>();
			for(Menu menu:children){
				childrens.add(buildtree(menu));
			}
			node.setChildren(childrens);
		}
		return node;
	}
	public MenuTreeNode buildtree(Menu m,String roleid) throws SQLException{//递归生成带角色的权限树
		MenuTreeNode node = new MenuTreeNode(m);
		if(roleid!=null){
			node.setChecked(m.hasRole(roleid));
		}
		List<Menu> children = new ArrayList<Menu>();
		children = m.findChildren();
		if(children.size()!=0){
			List<MenuTreeNode> childrens = new ArrayList<MenuTreeNode>();
			for(Menu menu:children){
				MenuTreeNode t=buildtree(menu,roleid);
				childrens.add(t);
			}
			node.setChildren(childrens);
		}
		return node;
	}

	public List<MenuTreeNode> getRet() {
		return ret;
	}
	public void setRet(List<MenuTreeNode> ret) {
		this.ret = ret;
	}


}
